/**
 * 时间戳转时间字符串
 * @param timestamp
 * @param hasHMS 是否返回时分秒
 * @returns {string}
 */
function dateFormat(timestamp, hasHMS=true) {
    if (timestamp === 0) {
        return '';
    }
    // console.log(timestamp)
    let time = new Date(timestamp * 1000);
    // console.log(time);
    let year = time.getFullYear();
    let month = time.getMonth() + 1;
    let day = time.getDate();
    let hour = time.getHours();
    let minute = time.getMinutes();
    let second = time.getSeconds();
    let result = '';
    if (hasHMS) {
        result = `${year}-${month}-${day} ${hour}:${minute}:${second}`;
    } else {
        result = `${year}-${month}-${day}`;
    }
    return result;
}

/**
 * 时间字符串转时间戳
 * @param datetime
 * @returns {number}
 */
function getTimeStamp(datetime) {
    if (datetime === '') {
        return 0;
    }
    return parseInt(new Date(datetime).getTime() / 1000);
}

/**
 * 格式化时间戳数组
 * @param timestampArr
 * @returns {[]}
 */
function dateFormatArr(timestampArr) {
    let datetimeArr = [];
    for (let timestamp of timestampArr) {
        datetimeArr.push(dateFormat(timestamp, false))
    }
    return datetimeArr;
}

/**
 * 获取当前时间字符串
 * @returns {string}
 */
function getDatetime() {
    let time = new Date();
    // console.log(time);
    let year = time.getFullYear();
    let month = time.getMonth() + 1;
    let day = time.getDate();
    let hour = time.getHours();
    let minute = time.getMinutes();
    let second = time.getSeconds();
    return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}

export {
    dateFormat,
    getTimeStamp,
    dateFormatArr,
    getDatetime
};

